﻿using TbShop.Data.Components;
using TbShop.Entity;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TbShop.Business.Components
{
    public class AspNetRoleBC
    {
        private TbShopDBContext DBContext = new TbShopDBContext();
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        public IQueryable<AspNetRoles> GetAll()
        {
            return DBContext.AspNetRoles.Where(a => !a.IsDeleted).OrderByDescending(p => p.Id);
        }
        /// <summary>
        /// 获取所有权限带分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<AspNetRoles> GetPagedList(int pageIndex, int pageSize)
        {
            return GetAll().OrderByDescending(x => x.Id).ToPagedList(pageIndex, pageSize);
        }
        /// <summary>
        /// 根据ID获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AspNetRoles Get(string id)
        {
            return GetAll().SingleOrDefault(c => c.Id == id);
        }
        /// <summary>
        /// 判断角色名称是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CheckName(string id, string name)
        {
            return !this.GetAll().Where(p => id != p.Id && p.Name == name && !p.IsDeleted).Any();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public AspNetRoles Save(AspNetRoles item)
        {
            if (!string.IsNullOrWhiteSpace(item.Id))
            {
                var res = DBContext.AspNetRoles.Attach(item);
                DBContext.Entry(item).State = System.Data.Entity.EntityState.Modified;
                return DBContext.SaveChanges() > 0 ? res : null;
            }
            else
            {
                item.Id = Guid.NewGuid().ToString("D");
                var res = DBContext.AspNetRoles.Add(item);
                return DBContext.SaveChanges() > 0 ? res : null;
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="versionNumber"></param>
        /// <returns></returns>
        public AspNetRoles Remove(string id, int versionNumber)
        {
            var item = this.Get(id);
            if (versionNumber == item.VersionNumber)
            {
                item.IsDeleted = true;
                DBContext.SaveChanges();
                return item;
            }
            else
            {
                throw new CustomerException.VersioNumberException();
            }
        }
    }
}
